//
//  WLSocketWorker.h
//  WanLian
//
//  Created by 123456 on 2018/12/26.
//  Copyright © 2018 wanlian. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "WLLocalFileHandle.h"
#import "WLSocketPacketParser.h"
NS_ASSUME_NONNULL_BEGIN

@class WLFileRequest;
@class WLRecallModel;
@class WLFileTransportHeadModel;

extern BOOL flag;
static long WriteFileHeadTag = 100001;
static long WriteFileBlockHeadTag = 100002;
static long WriteFileDataTag = 100003;

static long GetFileDataTag = 100004;
static long GetFileHeadTag = 100005;
static long GetFileCompleteTag = 100006;


static int FileHeadType = 0;
static int FileBlockHeadType = 255;
static long SocketTimeOut = -1;
#define SERVER_IP   "127.0.0.1"     //server IP
#define PORT 10000                  //Server端口
#define THREAD_NUM  4               //线程池大小
#define FILENAME_MAXLEN   30        //文件名最大长度
#define INT_SIZE    4               //int类型长度

typedef void (^FileHeadInfoSendSucBlock)(int sourceId);
typedef void (^FileBlockHeadInfoSendSucBlock)(int sourceId);
typedef void (^FileSocketConnectResultBlock)(int connectResult);
typedef void (^FileTransportFailureBlock)(NSString *msg, NSInteger errorTag);
typedef void (^FileTransportingBlock)(void);
typedef void (^FileTransportCompleteBlock)(void);
@interface WLSocketWorker : NSObject
{
    dispatch_queue_t workerSocketQueue;
}
@property (nonatomic, strong) WLRecallModel *recallModel;
@property (nonatomic, strong) WLFileRequest *requestModel;
@property (nonatomic, strong) GCDAsyncSocket *currentSocket;
@property (nonatomic, strong) NSFileHandle *fileHandle;
@property (nonatomic, strong) WLLocalFileHandle *localFileHandle;
@property (nonatomic, strong) WLFileTransportHeadModel *curHeadModel;
@property (nonatomic, copy) NSString *workerSocketQueueName;
@property (nonatomic, strong) WLSocketPacketParser *parser;
@property (nonatomic, copy) FileHeadInfoSendSucBlock fileHeadInfoSendSucBlock;
@property (nonatomic, copy) FileBlockHeadInfoSendSucBlock fileBlockHeadInfoSendSucBlock;
@property (nonatomic, copy) FileSocketConnectResultBlock fileSocketConnectResultBlock;
@property (nonatomic, copy) FileTransportFailureBlock fileTransportFailureBlock;
@property (nonatomic, copy) FileTransportingBlock fileTransportingBlock;
@property (nonatomic, copy) FileTransportCompleteBlock fileTransportCompleteBlock;



- (WLSocketWorker *)configWorkerWithFIFOModel:(WLFileRequest *)model
                         progress:(void (^)(float))progress
                          success:(void (^)(NSDictionary *successDict))success
                          failure:(void (^)(NSString *errorMsg, NSInteger errorTag))failure
                            error:(void (^)(NSError *error))err;

- (WLSocketWorker *)configDownloadWorkerWithFIFOModel:(WLFileRequest *)model
                                             progress:(void (^)(float process, long expected, long loaded))progress
                                              success:(void (^)(NSDictionary *successDict))success
                                              failure:(void (^)(NSString *errorMsg, NSInteger errorTag))failure
                                                error:(void (^)(NSError *error))err;
- (GCDAsyncSocket *)Client_init;

- (NSData *)configHeadProtocolWithData:(NSData *)data;
/* 操作 **/
- (void)suspend;

- (void)cancel;

- (void)resume;
- (void)clearTimer;
- (void)connectWithFailBlock:(void (^)(void))failBlock;
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;

/**
 * Called when a socket has completed reading the requested data into memory.
 * Not called if there is an error.
 **/
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
/**
 * Called when a socket has completed writing the requested data. Not called if there is an error.
 **/
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;

@end

NS_ASSUME_NONNULL_END
